﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Foundation.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Foundation.Component
{
    public class FoundationComponet : ComponentBase
    {
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <returns>是否添加成功</returns>
        public bool Add(FoundationEntity Details)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Foundation_ADD", Details) > 0);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyid">Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Foundation_Delete", null, keyid) > 0);
        }

        /// <summary>
        ///根据类别keyid
        /// </summary>
        /// <param name="keyid">Keyid</param> 
        public bool DeleteByFouKeyid(string keyids)
        {

            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Foundation_DeleteByFouKeyid", null, keyids) == count);
        }

        /// <summary>
        /// 根据过滤条件查询
        /// </summary>
        /// <param name="filterConditions">条件值</param>
        /// <returns>查询的集合</returns>
        public List<FoundationEntity> DoSelectByFoundation(string filterConditions)
        {
            NameValueCollection fieldConllection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldConllection, "FoundationClassKeyid", DbType.String, "AND FoundationClassKeyid like '%{0}%'"));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldConllection, "FoundationText", DbType.String, " AND FoundationText like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldConllection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<FoundationEntity>("sgrf_foundation_Foundation_DoSelectByConditions", null, filterConditions);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="keyids">keyids</param>
        /// <returns>批量删除是否成功</returns>
        public bool BulkDelete(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Foundation_BulkDelete", null, keyids) == count);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="UserDetails">一条记录</param>
        /// <returns>是否更新成功</returns>
        public bool Update(FoundationEntity Details)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Foundation_Update", Details) > 0);
        }

        /// <summary>
        /// 根据Keyid获取
        /// </summary>
        /// <param name="keyid">Keyid</param>
        /// <returns></returns>
        public FoundationEntity GetByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<FoundationEntity>("sgrf_foundation_Foundation_GetByKeyid", null, keyid);
        }
        /// <summary>
        /// 根据基础资料类别的keyid 
        /// </summary>
        /// <param name="keyid">keyid</param>
        /// <returns>对应的类别的所有集合</returns>
        public List<FoundationEntity> GetByFoundationClassKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<FoundationEntity>("sgrf_foundation_Foundation_GetByFoundataionClassKeyid", null, keyid);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns>集合</returns>
        public List<FoundationEntity> GetAll()
        {
            return FishBLLHelper.CallSpGetDataItemList<FoundationEntity>("sgrf_foundation_Foundation_GetAll", null);
        }

        /// <summary>
        /// 进行过滤、排序、分页查询
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>记录集合</returns>
        public List<FoundationEntity> PagingGet(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = "sgrf_Foundation_Foundation.[Order] ASC ";
            }

            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "1", DbType.String, " AND 1 = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationClassKeyid", DbType.String, " AND FoundationClassKeyid = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationText", DbType.String, " AND FoundationText like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationValue", DbType.String, " AND FoundationValue like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FieldA", DbType.String, " AND FieldA like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FieldB", DbType.String, " AND FieldB like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FieldC", DbType.String, " AND FieldB like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Remark", DbType.String, " AND FieldB like '%{0}%' "));
         
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            filterConditions = sql.ToString();

            return FishBLLHelper.CallSpGetDataItemList<FoundationEntity>("sgrf_foundation_Foundation_PagingGetFoundation", null, startRecord, maxRecords, sortColumns, filterConditions);
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {
                    case "foundationclasskeyid":
                    case "foundationtext":
                    case "foundationvalue":
                    case "mobilephone":
                    case "createdate":
                    case "issys":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤、排序、分页查询记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int PagingGetCount(String filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "1", DbType.String, " AND 1 = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationClassKeyid", DbType.String, " AND FoundationClassKeyid = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationText", DbType.String, " AND FoundationText like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationValue", DbType.String, " AND FoundationValue like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FieldA", DbType.String, " AND FieldA like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FieldB", DbType.String, " AND FieldB like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FieldC", DbType.String, " AND FieldB like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Remark", DbType.String, " AND FieldB like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            filterConditions = sql.ToString();

            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_foundation_Foundation_PagingGetFoundationCount", null, filterConditions));
        }

        public List<FoundationEntity> GetFoundationByClassCode(string classCode)
        {
            return FishBLLHelper.CallSpGetDataItemList<FoundationEntity>("sgrf_foundation_Foundation_GetFoundationByClassCode", null, classCode);
        }
    }
}
